package com.wenli.graduate2022.wenliGreenShop.controller;

import com.wenli.graduate2022.wenliGreenShop.entity.Product;
import com.wenli.graduate2022.wenliGreenShop.entity.ProductImage;
import com.wenli.graduate2022.wenliGreenShop.entity.ProductOrderItem;
import com.wenli.graduate2022.wenliGreenShop.entity.ShoppingCart;
import com.wenli.graduate2022.wenliGreenShop.service.servDao.ProductImageService;
import com.wenli.graduate2022.wenliGreenShop.service.servDao.ProductOrderItemService;
import com.wenli.graduate2022.wenliGreenShop.service.servDao.ShoppingCartService;
import com.wenli.graduate2022.wenliGreenShop.service.servImpl.ProductServImpl;
import com.wenli.graduate2022.wenliGreenShop.service.servImpl.ShoppingCartServImpl;
import com.wenli.graduate2022.wenliGreenShop.service.servImpl.UserServImpl;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author HuangHai
 * @date 2021/10/13 16:42
 */
@RestController
public class ShoppingCartController {

    @Autowired
    ShoppingCartService shoppingCartService;

    @Autowired
    ProductServImpl productServ;

    @Autowired
    UserServImpl userServ;


    @Autowired
    ProductImageService productImageService;

    protected Logger logger = LogManager.getLogger(ShoppingCart.class);

    @GetMapping("/shoppingCart/findByUserId/{id}")
    public List<ShoppingCart> findByUserId(@PathVariable("id")Integer id) {
        List<ShoppingCart> shoppingCarts =  shoppingCartService.findByUserId(id);
        for (ShoppingCart shoppingCart : shoppingCarts) {
            List<ProductImage> productImages = productImageService.findByType(shoppingCart.getShoppingcartProduct().getProductId(), 0);
            shoppingCart.setImageSrc(productImages.get(0).getProductImageSrc());
        }
        return shoppingCarts;
    }

    @PostMapping("/shoppingCart/addShoppingCart")
    public int addShoppingCart(@RequestBody Map<String,Object> reqBody) {      //根据id查询产品订单
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setShoppingcartNumber(((Integer) reqBody.get("number")).shortValue());
        shoppingCart.setShoppingcartPrice(((Integer) reqBody.get("price")).doubleValue());
        shoppingCart.setShoppingcartProduct(productServ.findById((Integer) reqBody.get("productId")));
        shoppingCart.setShoppingcartUser(userServ.findById(Integer.parseInt((String) reqBody.get("userId"))));
        List<ShoppingCart> byUserId = shoppingCartService.findByUserId(shoppingCart.getShoppingcartUser().getUserId());
        if (byUserId != null){ //如果之前收藏过就不插入了
            for (ShoppingCart shoppingCart1 : byUserId){
                if (shoppingCart1.getShoppingcartProduct().getProductId() == (Integer)reqBody.get("productId") ){
                   Short number =  shoppingCart1.getShoppingcartNumber();
                  shoppingCart.setShoppingcartNumber((short)(number+1));
                  logger.info("购物车重复加入，数量加一，不插入");
                    return shoppingCartService.updateShoppingCart(shoppingCart);
                }
            }
        }
        logger.info("购物车新的加入，插入");
        return shoppingCartService.addShoppingCart(shoppingCart);

    }

    public int updateShoppingCart(@RequestBody Map<String,Object> reqBody) {       //根据用户id查询该用户下的所有产品订单
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setShoppingcartNumber((Short) reqBody.get("number"));
        shoppingCart.setShoppingcartPrice((Double) reqBody.get("price"));
        shoppingCart.setShoppingcartProduct(productServ.findById((Integer) reqBody.get("productId")));
        shoppingCart.setShoppingcartUser(userServ.findById((Integer) reqBody.get("userId")));
        return shoppingCartService.updateShoppingCart(shoppingCart);
    }

    @DeleteMapping("/shoppingCart/deleteByUserId/{id}")
    public int deleteByUserId(@PathVariable("id")Integer id) {
        return shoppingCartService.deleteShoppingCart(id);
    }




}
